package org.jeecg.modules.business.qyxx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.ProvinceCityArea;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.business.qyxx.entity.BuGsfwptQyxx;
import org.jeecg.modules.business.qyxx.service.IBuGsfwptQyxxService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelVerifyEntity;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 企业信息表
 * @Author: jeecg-boot
 * @Date:   2023-04-01
 * @Version: V1.0
 */
@Api(tags="企业信息表")
@RestController
@RequestMapping("/qyxx/buGsfwptQyxx")
@Slf4j
public class BuGsfwptQyxxController extends JeecgController<BuGsfwptQyxx, IBuGsfwptQyxxService> {
	@Autowired
	private IBuGsfwptQyxxService buGsfwptQyxxService;
	
	/**
	 * 分页列表查询
	 *
	 * @param buGsfwptQyxx
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "企业信息表-分页列表查询")
	@ApiOperation(value="企业信息表-分页列表查询", notes="企业信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BuGsfwptQyxx buGsfwptQyxx,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BuGsfwptQyxx> queryWrapper = QueryGenerator.initQueryWrapper(buGsfwptQyxx, req.getParameterMap());
		Page<BuGsfwptQyxx> page = new Page<BuGsfwptQyxx>(pageNo, pageSize);
		IPage<BuGsfwptQyxx> pageList = buGsfwptQyxxService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param buGsfwptQyxx
	 * @return
	 */
	@AutoLog(value = "企业信息表-添加")
	@ApiOperation(value="企业信息表-添加", notes="企业信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BuGsfwptQyxx buGsfwptQyxx) {
		String code = buGsfwptQyxx.getSss() ;
		if(StringUtils.isNotBlank(code)){
			buGsfwptQyxx.setSsss(code) ;
		}
		buGsfwptQyxxService.save(buGsfwptQyxx);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param buGsfwptQyxx
	 * @return
	 */
	@AutoLog(value = "企业信息表-编辑")
	@ApiOperation(value="企业信息表-编辑", notes="企业信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BuGsfwptQyxx buGsfwptQyxx) {
		String code = buGsfwptQyxx.getSss() ;
		if(StringUtils.isNotBlank(code)){
			buGsfwptQyxx.setSsss(code) ;
		}
		buGsfwptQyxxService.updateById(buGsfwptQyxx);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业信息表-通过id删除")
	@ApiOperation(value="企业信息表-通过id删除", notes="企业信息表-通过id删除")
	@RequestMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		buGsfwptQyxxService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业信息表-批量删除")
	@ApiOperation(value="企业信息表-批量删除", notes="企业信息表-批量删除")
	@RequestMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.buGsfwptQyxxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业信息表-通过id查询")
	@ApiOperation(value="企业信息表-通过id查询", notes="企业信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BuGsfwptQyxx buGsfwptQyxx = buGsfwptQyxxService.getById(id);
		if(buGsfwptQyxx==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(buGsfwptQyxx);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param buGsfwptQyxx
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BuGsfwptQyxx buGsfwptQyxx) {
		// Step.1 组装查询条件
		QueryWrapper<BuGsfwptQyxx> queryWrapper = QueryGenerator.initQueryWrapper(buGsfwptQyxx, request.getParameterMap());

		// Step.2 获取导出数据
		List<BuGsfwptQyxx> pageList = service.list(queryWrapper);
		ProvinceCityArea pa = new ProvinceCityArea() ;
		for(BuGsfwptQyxx bq : pageList){

			if (StringUtils.isNotBlank(bq.getSss())){
				String sss = pa.getText(bq.getSss()).split("/")[0] ;
				bq.setSss(sss) ;
			}
			if (StringUtils.isNotBlank(bq.getSss())){
				String ssss = pa.getText(bq.getSsss()).split("/")[1] ;
				bq.setSsss(ssss) ;
			}

		}
        return super.customRyglExportXls(request, pageList, BuGsfwptQyxx.class, "企业信息表");
		// return super.exportXls(request, buGsfwptQyxx, BuGsfwptQyxx.class, "企业信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			//   return super.importExcel(request, response, BuGsfwptQyxx.class);
			ExcelImportResult<ExcelVerifyEntity> result = ExcelImportUtil.importExcelVerify(file.getInputStream(), ExcelVerifyEntity.class, params);
                /*for (int i = 0; i < result.getList().size(); i++) {
                    System.out.println(ReflectionToStringBuilder.toString(result.getList().get(i)));
                }*/
			ProvinceCityArea pa = new ProvinceCityArea() ;
			List<BuGsfwptQyxx> list = ExcelImportUtil.importExcel(file.getInputStream(), BuGsfwptQyxx.class, params);
			for (BuGsfwptQyxx bq : list) {
				String sss = bq.getSss() ;
				String ssss = bq.getSsss() ;

				if(StringUtils.isNotBlank(sss)&& StringUtils.isNotBlank(ssss)){
					if(ssss.equals("市辖区")){
						String scode = pa.getCode(sss+"/"+ssss) ;
						if(StringUtils.isNotBlank(scode)){
							bq.setSss(scode) ;
							bq.setSsss(scode) ;
						}
					}else {
						String scode = pa.getCode(ssss) ;
						if(StringUtils.isNotBlank(scode)){
							bq.setSss(scode) ;
							bq.setSsss(scode) ;
						}
					}

				}

			}
			return super.importExcelQyxx(file, list);
		}
		return Result.error("文件导入失败！");

	}

}
